package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleInformation;
import com.caigou.mapper.cms.ArticleInformationMapper;
import com.caigou.service.ArticleInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleInformationServiceImpl implements ArticleInformationService {

    @Resource
    private ArticleInformationMapper articleInformationMapper;

    @Override
    public ArticleInformation saveArticleInformationData(String articleId, String articleContent) {
        ArticleInformation articleInformation = new ArticleInformation();
        articleInformation.setArticleId(articleId);
        articleInformation.setArticleContent(articleContent);
        articleInformationMapper.saveArticleInformationData(articleInformation);
        return articleInformation;
    }

    @Override
    public int deleteArticleInformationData(String articleId) {
        return articleInformationMapper.deleteArticleInformationData(articleId);
    }

    @Override
    public int updateArticleInformationData(String articleId, String articleContent) {
        ArticleInformation articleInformation = articleInformationMapper.loadArticleInformationData(articleId);
        boolean isCreated = false;
        if (null == articleInformation){
            isCreated = true;
            articleInformation = new ArticleInformation();
        }
        articleInformation.setArticleId(articleId);
        articleInformation.setArticleContent(articleContent);
        if (isCreated){
            return articleInformationMapper.saveArticleInformationData(articleInformation);
        } else {
            return articleInformationMapper.updateArticleInformationData(articleInformation);
        }
    }

    @Override
    public ArticleInformation loadArticleInformationData(String articleId) {
        return articleInformationMapper.loadArticleInformationData(articleId);
    }

    @Override
    public List<ArticleInformation> loadDisponseFinalDataList(String id, Integer limit) {
        return articleInformationMapper.loadDisponseFinalDataList(id,limit);
    }
}
